# 在 QueryDict 新增数据并返回 QueryDict 类型
def QueryDict_Add(data, key, value):
    if key == '':
        return value
    else:
        # 记住旧的方式
        _mutable = data._mutable
        # 设置_mutable为True
        data._mutable = True
        data[key] = value

        # 恢复_mutable原来的属性
        data._mutable = _mutable
    return data



# 获取 forms ModelForm 中的错误信息， 返回元组(字段名， 错误信息)
def Forms_GetError(form):
    return [(field, errors[0]) for field, errors in form.errors.items()][0]


# 返回api数据
def ReturnData(code = 200,  msg = 'success',data=None, template=0):
    if template == 0:
        return {
            "code": code,
            "msg": msg,
            "data": data
        }
    elif template == 1:
        return {
            "code": 400,
            "msg": msg if msg == 'success' else '请求方式错误',
            "data": None
        }


